﻿Imports Q2Q.UI
Imports System.ComponentModel

Namespace Controls
    Public Class CueList

#Region "Basic Functionality"

        ''' <summary>
        ''' Raised when app settings are changed, updates font sizes if need-be.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub SettingsChanged(sender As Object, e As System.Configuration.SettingChangingEventArgs)
            If e.SettingName = "DefaultCueFontSize" Then
                DefaultCueFontSize = e.NewValue
            ElseIf e.SettingName.Contains("Column") Then
                My.Settings.Save()
            End If
        End Sub


        ''' <summary>
        ''' Creates a new cue list.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            ' This call is required by the designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            AddHandler My.Settings.SettingChanging, AddressOf SettingsChanged

        End Sub

        ''' <summary>
        ''' Clears selections from the list if the list is clicked away from.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub CueList_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseUp
            If e.OriginalSource.GetType Is GetType(ScrollViewer) Then
                Cues.SelectedIndex = -1
            End If
        End Sub

        ''' <summary>
        ''' Cleanup when the list is unloaded from memory.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub CueList_Unloaded(sender As Object, e As RoutedEventArgs) Handles Me.Unloaded
            RemoveHandler My.Settings.SettingChanging, AddressOf SettingsChanged
        End Sub

        ''' <summary>
        ''' Provides access to the application setting for XAML.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property SettingsAccess As Object
            Get
                Return My.Settings
            End Get
            Set(value As Object)
            End Set
        End Property

        ''' <summary>
        ''' Gets or Sets the CueList that are displayed in the control.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property CueListing As Collections.CueList
            Get
                Return GetValue(CueListingProperty)
            End Get

            Set(ByVal value As Collections.CueList)
                SetValue(CueListingProperty, value)
            End Set
        End Property

        ''' <summary>
        ''' The dependency property for the CueListing property.
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared ReadOnly CueListingProperty As DependencyProperty =
                               DependencyProperty.Register("CueListing",
                               GetType(Collections.CueList), GetType(CueList),
                               New PropertyMetadata(New Collections.CueList))

        ''' <summary>
        ''' Adds a cue to the cue list.
        ''' </summary>
        ''' <param name="Cue">The cue to add to the list.</param>
        ''' <remarks></remarks>
        Public Sub AddCueToList(Cue As Cues.CueBase)
            CueListing.AddCue(Cue)
        End Sub

        ''' <summary>
        ''' If greater than -1 indicates that it is owned by another cue, normally a StackCue.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ParentCueNumber As Double
            Get
                Return GetValue(ParentCueNumberProperty)
            End Get

            Set(ByVal value As Double)
                SetValue(ParentCueNumberProperty, value)
            End Set
        End Property

        ''' <summary>
        ''' Represents the dependency property for the ParentCueNumber Property.
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared ReadOnly ParentCueNumberProperty As DependencyProperty =
                               DependencyProperty.Register("ParentCueNumber",
                               GetType(Double), GetType(CueList),
                               New PropertyMetadata(Double.Parse("0")))


        ''' <summary>
        ''' Indicates whether certain elements are enabled for editing on cues.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property IsEditingEnabled As Boolean
            Get
                Return GetValue(IsEditingEnabledProperty)
            End Get

            Set(ByVal value As Boolean)
                SetValue(IsEditingEnabledProperty, value)
            End Set
        End Property

        ''' <summary>
        ''' Represents the dependency property for the EditingEnabled Property
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared ReadOnly IsEditingEnabledProperty As DependencyProperty =
                               DependencyProperty.Register("IsEditingEnabled",
                               GetType(Boolean), GetType(CueList),
                               New PropertyMetadata(True))





        ''' <summary>
        ''' Returns the collection of SelectedItems from the control's internal list.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property SelectedItems As System.Collections.IList
            Get
                Return Cues.SelectedItems
            End Get
        End Property

        ''' <summary>
        ''' Returns the first item from the control's internal SelectedItems list.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property SelectedItem As Object
            Get
                Return Cues.SelectedItem
            End Get
        End Property

        ''' <summary>
        ''' Returns the index of the first selected item in the control's collection.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property SelectedIndex As Integer
            Get
                Return Cues.SelectedIndex
            End Get
            Set(value As Integer)
                Cues.SelectedIndex = value
            End Set
        End Property


        ''' <summary>
        ''' Attempts to scroll the selected item(s) into view.
        ''' </summary>
        ''' <param name="item"></param>
        ''' <remarks></remarks>
        Public Sub ScrollIntoView(item As Object)
            Cues.ScrollIntoView(item)
        End Sub

        ''' <summary>
        ''' Sets the font size of the cue interfaces.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property DefaultCueFontSize As Double
            Get
                Return GetValue(CueList.DefaultCueFontSizeProperty)
            End Get

            Set(ByVal value As Double)
                SetValue(CueList.DefaultCueFontSizeProperty, value)
            End Set
        End Property

        ''' <summary>
        ''' Represents the DefaultCueFontSize property.
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared ReadOnly DefaultCueFontSizeProperty As DependencyProperty =
                               DependencyProperty.Register("DefaultCueFontSize",
                               GetType(Double), GetType(CueList),
                               New PropertyMetadata(My.Settings.DefaultCueFontSize))


#End Region


#Region "Commands"

#Region "Common Can Execute Qualifiers"

        ''' <summary>
        ''' Sets CanExecute to true as long as at least one item is selected.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub CanExecuteWithAtLeastOneSelected(sender As Object, e As CanExecuteRoutedEventArgs)
            Try
                If Cues.SelectedItems.Count > 0 Then
                    e.CanExecute = True
                Else
                    e.CanExecute = False
                End If
            Catch ex As Exception
                e.CanExecute = False
            End Try
            e.Handled = True
        End Sub

        ''' <summary>
        ''' Sets CanExecute to true as long as at least one item is selected and paste data is valid.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub CanExecuteWithAtLeastOneSelectedPaste(sender As Object, e As CanExecuteRoutedEventArgs)
            If Cues.SelectedItems.Count > 0 AndAlso My.Computer.Clipboard.ContainsData("Q2CueCopyCueData") Then
                e.CanExecute = True
            Else
                e.CanExecute = False
            End If
            e.Handled = True
        End Sub

        ''' <summary>
        ''' Sets CanExecute to true if a single item is selected.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub CanExecuteWithOnlyOneSelected(sender As Object, e As CanExecuteRoutedEventArgs)
            If Cues.SelectedItems.Count = 1 Then
                e.CanExecute = True
            Else
                e.CanExecute = False
            End If
            e.Handled = True
        End Sub

        Public Sub CanExecuteEditing(sender As Object, e As CanExecuteRoutedEventArgs)
            If IsEditingEnabled Then
                e.CanExecute = True
            Else
                e.CanExecute = False
            End If
            e.Handled = True
        End Sub
#End Region

#Region "Delete"

        ''' <summary>
        ''' Deletes the cue or cues selected in the list.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub ExecuteDeleteCue(sender As Object, e As ExecutedRoutedEventArgs)
            'Todo:Fix deleting cues when multiple cues are selected. The current implementations either delete all from the master cue list and nothing from stack cues, or can't delete multiple anywhere. Commands aren't routing?
            If MsgBox("Are you sure you want to delete the selected cues? This action cannot be undone.", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Return
            End If
            'Dim cueSender As Cues.CueBase = CType(e.Parameter, Cues.CueBase)
            'If cueSender.ParentCue <> Nothing AndAlso cueSender.ParentCue <> "" AndAlso cueSender.ParentCue <> "0" AndAlso cueSender.ParentCue <> "-1" Then
            '	Dim num() As String = cueSender.FullNumber.Split(":")
            '	Dim cue As Cues.CueBase = Nothing
            '	For i = 0 To num.Length - 1
            '		If cue Is Nothing Then
            '			cue = My.Application.Show.ShowFile.CueList.FindCueByNumber(num(i))
            '		ElseIf cue.GetType Is GetType(Cues.Stack) Then
            '			If i = num.Length - 1 Then
            '				CType(cue, Cues.Stack).CueList.Remove(num(i))
            '				Return
            '			Else
            '				cue = CType(cue, Cues.Stack).CueList.FindCueByNumber(num(i))
            '			End If
            '		Else
            '			CueListing.Remove(num(i))
            '		End If
            '	Next
            'Else
            '	CueListing.Remove(cueSender.Number)
            'End If


            Dim l As New List(Of Double)
            'If CType(e.Parameter, Cues.CueBase).ParentCue <> 0 Then
            '    With CType(FindName("CueListStack"), CueList)
            '        For Each c As Cues.CueBase In .SelectedItems
            '            l.Add(c.Number)
            '        Next
            '        For Each ld In l
            '            .CueListing.Remove(ld)
            '        Next
            '    End With
            'Else
            For Each c As Cues.CueBase In Cues.SelectedItems
                    l.Add(c.Number)
                Next
                For Each ld In l
                    CueListing.Remove(ld)
                Next
            'End If

            e.Handled = True
        End Sub

#End Region

#Region "Copy"

        ''' <summary>
        ''' Copies the attributes from the selected cue.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub ExecuteCopyCue(sender As Object, e As ExecutedRoutedEventArgs)
            My.Computer.Clipboard.SetData("Q2CueCopyCueData", Cues.SelectedItem)
            e.Handled = True
        End Sub
#End Region

#Region "Paste"

        ''' <summary>
        ''' Pastes attributes to the selected cues.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub ExecutePasteCue(sender As Object, e As ExecutedRoutedEventArgs)
            'Try
            '    If My.Computer.Clipboard.GetData("Q2CueCopyCueData").GetType Is GetType(Cues.Audio) Then
            '        Dim cue As Cues.Audio = CType(My.Computer.Clipboard.GetData("Q2CueCopyCueData"), Cues.Audio)
            '        Dim pw As New PasteDialog(Q2Q.Cues.Types.Audio)
            '        pw.Owner = Window.GetWindow(Me)
            '        pw.ShowDialog()
            '        If pw.DialogResult = False Then
            '            Return
            '        End If
            '        For Each c As Cues.CueBase In Cues.SelectedItems
            '            If c.GetType Is GetType(Cues.Audio) Then
            '                With CType(c, Cues.Audio)
            '                    If pw.AudioNotes.IsChecked Then
            '                        CType(c, Cues.CueBase).Description = cue.Description
            '                    End If
            '                    If pw.AudioPostAction.IsChecked Then
            '                        CType(c, Cues.CueBase).TransportAction = cue.TransportAction
            '                    End If
            '                    If pw.AudioDelay.IsChecked Then
            '                        CType(c, Cues.CueBase).ActionDelay = cue.ActionDelay
            '                    End If
            '                    If pw.FadeIn.IsChecked Then
            '                        .FadeInDuration = cue.FadeInDuration
            '                    End If
            '                    If pw.FadeOut.IsChecked Then
            '                        .FadeOutDuration = cue.FadeOutDuration
            '                    End If
            '                    If pw.Volume.IsChecked Then
            '                        .Volume = cue.Volume
            '                    End If
            '                    If pw.Pan.IsChecked Then
            '                        .Pan = cue.Pan
            '                    End If
            '                End With
            '            ElseIf c.GetType Is GetType(Cues.Transport) Then
            '                If pw.AudioNotes.IsChecked Then
            '                    CType(c, Cues.CueBase).Description = cue.Description
            '                End If
            '                If pw.AudioPostAction.IsChecked Then
            '                    CType(c, Cues.CueBase).TransportAction = cue.TransportAction
            '                End If
            '                If pw.AudioDelay.IsChecked Then
            '                    CType(c, Cues.CueBase).ActionDelay = cue.ActionDelay
            '                End If
            '            ElseIf c.GetType Is GetType(Cues.Stack) Then
            '                If pw.StackNotes.IsChecked Then
            '                    CType(c, Cues.CueBase).Description = cue.Description
            '                End If
            '                If pw.StackPostAction.IsChecked Then
            '                    CType(c, Cues.CueBase).TransportAction = cue.TransportAction
            '                End If
            '                If pw.StackDelay.IsChecked Then
            '                    CType(c, Cues.CueBase).ActionDelay = cue.ActionDelay
            '                End If
            '            End If
            '        Next
            '    ElseIf My.Computer.Clipboard.GetData("Q2CueCopyCueData").GetType Is GetType(Cues.Transport) Then
            '        Dim cue As Cues.Transport = CType(My.Computer.Clipboard.GetData("Q2CueCopyCueData"), Cues.Transport)
            '        Dim pw As New PasteDialog(Q2Q.Cues.Types.Transport)
            '        pw.Owner = Window.GetWindow(Me)
            '        pw.ShowDialog()
            '        If pw.DialogResult = False Then
            '            Return
            '        End If
            '        For Each c As Cues.CueBase In Cues.SelectedItems
            '            If c.GetType Is GetType(Cues.Audio) Then
            '                With CType(c, Cues.Audio)
            '                    If pw.TransportNotes.IsChecked Then
            '                        CType(c, Cues.CueBase).Description = cue.Description
            '                    End If
            '                    If pw.TransportPostAction.IsChecked Then
            '                        CType(c, Cues.CueBase).TransportAction = cue.TransportAction
            '                    End If
            '                    If pw.TransportDelay.IsChecked Then
            '                        CType(c, Cues.CueBase).ActionDelay = cue.ActionDelay
            '                    End If
            '                End With
            '            ElseIf c.GetType Is GetType(Cues.Transport) Then
            '                With CType(c, Cues.Transport)
            '                    If pw.TransportNotes.IsChecked Then
            '                        CType(c, Cues.CueBase).Description = cue.Description
            '                    End If
            '                    If pw.TransportPostAction.IsChecked Then
            '                        CType(c, Cues.CueBase).TransportAction = cue.TransportAction
            '                    End If
            '                    If pw.TransportDelay.IsChecked Then
            '                        CType(c, Cues.CueBase).ActionDelay = cue.ActionDelay
            '                    End If
            '                    If pw.CueAction.IsChecked Then
            '                        .Action = cue.Action
            '                    End If
            '                    If pw.TargetCue.IsChecked Then
            '                        .TargetCue = cue.TargetCue
            '                    End If
            '                End With
            '            ElseIf c.GetType Is GetType(Cues.Stack) Then
            '                If pw.StackNotes.IsChecked Then
            '                    CType(c, Cues.CueBase).Description = cue.Description
            '                End If
            '                If pw.StackPostAction.IsChecked Then
            '                    CType(c, Cues.CueBase).TransportAction = cue.TransportAction
            '                End If
            '                If pw.StackDelay.IsChecked Then
            '                    CType(c, Cues.CueBase).ActionDelay = cue.ActionDelay
            '                End If
            '            End If
            '        Next
            '    ElseIf My.Computer.Clipboard.GetData("Q2CueCopyCueData").GetType Is GetType(Cues.Stack) Then
            '        Dim cue As Cues.Stack = CType(My.Computer.Clipboard.GetData("Q2CueCopyCueData"), Cues.Stack)
            '        Dim pw As New PasteDialog(Q2Q.Cues.Types.Stack)
            '        pw.Owner = Window.GetWindow(Me)
            '        pw.ShowDialog()
            '        If pw.DialogResult = False Then
            '            Return
            '        End If
            '        For Each c As Cues.CueBase In Cues.SelectedItems
            '            If pw.StackNotes.IsChecked Then
            '                CType(c, Cues.CueBase).Description = cue.Description
            '            End If
            '            If pw.StackPostAction.IsChecked Then
            '                CType(c, Cues.CueBase).TransportAction = cue.TransportAction
            '            End If
            '            If pw.StackDelay.IsChecked Then
            '                CType(c, Cues.CueBase).ActionDelay = cue.ActionDelay
            '            End If
            '            If pw.CueList.IsChecked Then
            '                CType(c, Cues.Stack).CueList = cue.CueList
            '            End If
            '        Next
            '    End If
            'Catch ex As Exception
            '    My.Application.Show.Console.SendMessage("There was a problem pasting data.", ex)
            'End Try

            If My.Computer.Clipboard.ContainsData("Q2CueCopyCueData") Then
                Q2Q.Cues.Helpers.Paste.PasteCueData(CType(My.Computer.Clipboard.GetData("Q2CueCopyCueData"), Cues.CueBase), My.Application.Show.ShowFile.CueList.GetSelectedCues)
            End If

            e.Handled = True
        End Sub
#End Region

#Region "Change Cue Number"
        ''' <summary>
        ''' Changes the cue number for the selected cue.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub ExecuteChangeCueNumber(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                Dim NumWin As New ChangeCueNumber()
                NumWin.Owner = Window.GetWindow(Me)
                NumWin.Number.Value = CType(e.Parameter, Cues.CueBase).Number
                If NumWin.ShowDialog = True Then
                    If CueListing.FindCueByNumber(NumWin.Number.Value) IsNot Nothing Then
                        If MsgBox("A cue with this number already exists. Do you want to overwrite this cue?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                            CueListing.Remove(NumWin.Number.Value)
                        Else
                            e.Handled = True
                            Return
                        End If
                    End If
                    CueListing.FindCueByNumber(CType(e.Parameter, Cues.CueBase).Number).Number = NumWin.Number.Value
                    CueListing.Sort()
                End If
            Catch ex As Exception
                e.Handled = True
            End Try
            e.Handled = True
        End Sub
#End Region
#Region "MultiChannel Settings"
        ''' <summary>
        ''' Opens the MultiChannelSettings dialog for the selected cue.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub ExecuteMultiChannelSettings(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                Dim cd As New CueMultiChannelSettings(CType(e.Parameter, Cues.CueBase))
                cd.Owner = Window.GetWindow(Me)
                cd.ShowDialog()
            Catch ex As Exception
            End Try
            e.Handled = True
        End Sub

        ''' <summary>
        ''' Determines if multichannel settings can be viewed for the current cue.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub CanExecuteMultiChannelSettings(sender As Object, e As CanExecuteRoutedEventArgs)
            If My.Settings.PlayerType = Audio.Players.PlayerTypes.Wasapi Then
                e.CanExecute = True
            Else
                e.CanExecute = False
            End If
            e.Handled = True
        End Sub
#End Region
#Region "Bump Status"
        ''' <summary>
        ''' Bumps the status of the selected cues.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub ExecuteBumpStatus(sender As Object, e As ExecutedRoutedEventArgs)
            For Each c In Cues.SelectedItems.OfType(Of Cues.CueBase)()
                c.BumpStatus()
            Next
            e.Handled = True
        End Sub
#End Region

#Region "Add Cues"

        ''' <summary>
        ''' Adds an empty audio cue to the list.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub ExecuteAddAudioCue(sender As Object, e As ExecutedRoutedEventArgs)
            If e.Parameter IsNot Nothing AndAlso e.Parameter.GetType Is GetType(Cues.Stack) Then
                CType(e.Parameter, Cues.Stack).CueList.AddCue(New Cues.Audio(CType(e.Parameter, Cues.Stack).FullNumber))
            ElseIf e.Parameter IsNot Nothing AndAlso e.Parameter.GetType Is GetType(CueList) Then
                CType(e.Parameter, CueList).CueListing.AddCue(New Cues.Audio(CType(e.Parameter, CueList).ParentCueNumber))
            Else
                CueListing.AddCue(New Cues.Audio(ParentCueNumber))
            End If
            e.Handled = True


        End Sub

        ''' <summary>
        ''' Opens a window to select audio files, allowing for adding large numbers of audio cues at once, preloaded with files.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Async Sub ExecuteBulkAddAudioCues(sender As Object, e As ExecutedRoutedEventArgs)
            If e.Parameter IsNot Nothing AndAlso e.Parameter.GetType Is GetType(Cues.Stack) Then
                For Each a In Await Q2Q.Cues.Audio.LoadMultipleFilesToCues(CType(e.Parameter, Cues.Stack).FullNumber)
                    CType(e.Parameter, Cues.Stack).CueList.AddCue(a)
                Next
            ElseIf e.Parameter IsNot Nothing AndAlso e.Parameter.GetType Is GetType(CueList) Then
                For Each a In Await Q2Q.Cues.Audio.LoadMultipleFilesToCues(CType(e.Parameter, CueList).ParentCueNumber)
                    CType(e.Parameter, CueList).CueListing.AddCue(a)
                Next
            Else
                For Each a In Await Q2Q.Cues.Audio.LoadMultipleFilesToCues(ParentCueNumber)
                    CueListing.AddCue(a)
                Next
            End If
            e.Handled = True
        End Sub

        ''' <summary>
        ''' Adds a new transport cue to the end of the list.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub ExecuteAddTransportCue(sender As Object, e As ExecutedRoutedEventArgs)
            If e.Parameter IsNot Nothing AndAlso e.Parameter.GetType Is GetType(Cues.Stack) Then
                CType(e.Parameter, Cues.Stack).CueList.AddCue(New Cues.Transport(CType(e.Parameter, Cues.Stack).FullNumber))
            ElseIf e.Parameter IsNot Nothing AndAlso e.Parameter.GetType Is GetType(CueList) Then
                CType(e.Parameter, CueList).CueListing.AddCue(New Cues.Transport(CType(e.Parameter, CueList).ParentCueNumber))
            Else
                CueListing.AddCue(New Cues.Transport(ParentCueNumber))
            End If
            e.Handled = True
        End Sub

        ''' <summary>
        ''' Adds a new stack to the end of the list.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub ExecuteAddStackCue(sender As Object, e As ExecutedRoutedEventArgs)
            If e.Parameter IsNot Nothing AndAlso e.Parameter.GetType Is GetType(Cues.Stack) Then
                CType(e.Parameter, Cues.Stack).CueList.AddCue(New Cues.Stack(CType(e.Parameter, Cues.Stack).FullNumber))
            ElseIf e.Parameter IsNot Nothing AndAlso e.Parameter.GetType Is GetType(CueList) Then
                CType(e.Parameter, CueList).CueListing.AddCue(New Cues.Stack(CType(e.Parameter, CueList).ParentCueNumber))
            Else
                CueListing.AddCue(New Cues.Stack(ParentCueNumber))
            End If
            e.Handled = True
        End Sub

#End Region

#Region "Transport Commands"
        Public Sub ExecutePlay(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                If e.Parameter.GetType Is GetType(Cues.Audio) Then
                    CType(e.Parameter, Cues.Audio).Play()
                ElseIf e.Parameter.GetType Is GetType(Cues.Stack) Then
                    CType(e.Parameter, Cues.Stack).Start()
                End If
            Catch ex As Exception
                My.Application.Show.Console.SendMessage("There was a problem playing a cue in the cue list.", ex)
            End Try
            e.Handled = True
        End Sub

        Public Sub ExecutePause(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                If e.Parameter.GetType Is GetType(Cues.Audio) Then
                    CType(e.Parameter, Cues.Audio).Pause()
                ElseIf e.Parameter.GetType Is GetType(Cues.Stack) Then
                    CType(e.Parameter, Cues.Stack).Pause()
                End If
            Catch ex As Exception
            End Try
            e.Handled = True
        End Sub

        Public Sub ExecuteStop(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                CType(e.Parameter, Cues.CueBase).Stop()
            Catch ex As Exception
            End Try
            e.Handled = True
        End Sub

        Public Sub ExecuteHardStop(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                If e.Parameter.GetType Is GetType(Cues.Audio) Then
                    CType(e.Parameter, Cues.Audio).Stop(False)
                ElseIf e.Parameter.GetType Is GetType(Cues.Stack) Then
                    CType(e.Parameter, Cues.Stack).Stop(False)
                End If
                e.Handled = True
            Catch ex As Exception
            End Try
        End Sub

        Public Sub ExecuteLoopCue(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                If e.Parameter.GetType Is GetType(Cues.Audio) Then
                    If CType(e.Parameter, Cues.Audio).EnableLooping Then
                        CType(e.Parameter, Cues.Audio).EnableLooping = False
                    Else
                        CType(e.Parameter, Cues.Audio).EnableLooping = True
                    End If
                End If
            Catch ex As Exception
            End Try
            e.Handled = True
        End Sub

        Public Sub ExecuteCenterPan(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                CType(e.Parameter, Cues.Audio).Pan = 0
            Catch ex As Exception
            End Try
            e.Handled = True
        End Sub

#End Region

#Region "Audio File Stuff"

        Public Sub ExecuteLoadAudioFile(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                CType(e.Parameter, Cues.Audio).LoadFile()
            Catch ex As Exception
            End Try
            e.Handled = True
        End Sub

#End Region

#Region "Stack Cue Toggle"
        Public Sub ExecuteToggleStackCueList(sender As Object, e As ExecutedRoutedEventArgs)
            Try
                Dim l As Q2Q.Controls.CueList = CType(CType(CType(e.OriginalSource, Button).Parent, Grid).FindName("CueListStack"), Q2Q.Controls.CueList)
                If l.Visibility = System.Windows.Visibility.Visible Then
                    l.Visibility = System.Windows.Visibility.Collapsed
                    CType(e.OriginalSource, Button).Content = "Show Cue List"
                Else
                    l.Visibility = System.Windows.Visibility.Visible
                    CType(e.OriginalSource, Button).Content = "Hide Cue List"
                End If
            Catch ex As Exception
            End Try
            e.Handled = True
        End Sub
#End Region

#End Region

        ''' <summary>
        ''' Forces a visual update. Used when changing themes.
        ''' </summary>
        ''' <param name="CueList"></param>
        ''' <remarks></remarks>
        Public Shared Sub ForceVisualUpdate(CueList As Collections.CueList)
            For Each c In CueList
                c.RaisePropertyChanged("Status")
                If c.GetType Is GetType(Cues.Stack) Then
                    ForceVisualUpdate(CType(c, Cues.Stack).CueList)
                End If
            Next
        End Sub


        ''' <summary>
        ''' Checks for the Spacebar GO command.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub DesignWindow_PreviewKeyDown(sender As Object, e As KeyEventArgs) Handles Me.PreviewKeyDown
            If CType(My.Application.MainWindow, DesignWindow).IsInShowMode AndAlso e.Key = Key.Space Then
                'My.Application.Show.Go()
                My.Application.Show.ShowFile.CueList.Go()
                If Cues.Visibility = System.Windows.Visibility.Visible Then
                    Try
                        Cues.ScrollIntoView(My.Application.Show.ShowFile.LastActiveCue())
                    Catch
                    End Try
                End If
                e.Handled = True
            End If
        End Sub

        '''' <summary>
        '''' Forces focus on the ListView element to ensure that commands work. /Hack
        '''' </summary>
        '''' <param name="sender"></param>
        '''' <param name="e"></param>
        '''' <remarks></remarks>
        'Private Sub Cues_MouseEnter(sender As Object, e As MouseEventArgs) Handles Cues.MouseEnter
        'Cues.Focus()
        'End Sub

        '''' <summary>
        '''' Clears selections from the main cue list of cues in sub-lists are selected.
        '''' This doesn't completely solve selection issues.
        '''' </summary>
        '''' <param name="sender"></param>
        '''' <param name="e"></param>
        '''' <remarks></remarks>
        'Private Sub Cues_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles Cues.SelectionChanged
        '    Try
        '        If e.AddedItems.Count > 0 Then
        '            Dim n As String = CType(e.AddedItems.Item(0), Cues.CueBase).ParentCue
        '            If n.Length > 0 AndAlso n <> "0" AndAlso n <> "-1" Then
        '                CType(My.Application.MainWindow, DesignWindow).Cues.SelectedIndex = -1
        '            End If
        '        End If
        '    Catch
        '    End Try
        'End Sub
    End Class




End Namespace
